/**
 * @file
 * Javascript behaviors for the Flagdispay theme.
 */

(function($) {

  Drupal.behaviors.imageZoom = {
    attach: function() {
      // Variable settings, distance of zoom image from mouse pointer
      xOffset = 20;
      yOffset = -30;
      // Attaching mouseover event
      $('img.zoom').mouseover(function(e) {
        $('body').append('<p id="zoom"><img src="' + $(this).attr('rel') + '" /></p>');
        var x = e.pageX + xOffset, y = e.pageY + yOffset;
        var tipw=$('#zoom').width(), tiph=$('#zoom').height();
        var x=(x + tipw > $(document).scrollLeft() + $(window).width())? x-tipw-(xOffset*2) : x;
        var y=(y + tiph > $(document).scrollTop() + $(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y;
        $('#zoom').css('top', y + 'px').css('left', x + 'px').fadeIn('slow');
      });
      // Attaching mousemove event
      $('img.zoom').mousemove(function(e) {
        var x = e.pageX + xOffset, y = e.pageY + yOffset;
        var tipw=$('#zoom').width(), tiph=$('#zoom').height();
        var x=(x + tipw > $(document).scrollLeft() + $(window).width())? x-tipw-(xOffset*2) : x;
        var y=(y + tiph > $(document).scrollTop() + $(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y;
        $('#zoom').css('top', y + 'px').css('left', x + 'px').fadeIn('slow');
      });
      // Attaching mouseleave event.
      $('img.zoom').mouseleave(function(e) {
        $('#zoom').remove();
      });
    }
  };

})(jQuery);
